
//
//  DevicesListCell.swift
//  MIFI
//
//  Created by 金嗓子卡卡 on 2019/4/21.
//  Copyright © 2019 NJMifi. All rights reserved.
//

import UIKit

class DevicesListCell: UITableViewCell {
    private lazy var imageV: UIImageView = {
        let imageV = UIImageView(image: UIImage(named: "device"))
        imageV.backgroundColor = clearColor
        imageV.contentMode = .scaleAspectFill
        return imageV
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.backgroundColor = clearColor
        label.textColor = whiteColor
        label.font = UIFont.systemFont(ofSize: 20)
        return label
    }()
    
    private lazy var ipLabel: UILabel = {
        let label = UILabel()
        label.backgroundColor = clearColor
        label.numberOfLines = 2
        label.textColor = textGray
        label.font = UIFont.systemFont(ofSize: 18)
        return label
    }()
    
    private lazy var macLabel: UILabel = {
        let label = UILabel()
        label.backgroundColor = clearColor
        label.numberOfLines = 2
        label.textColor = ipLabel.textColor
        label.font = ipLabel.font
        return label
    }()
    
    private lazy var statusLabel: UILabel = {
        let label = UILabel()
        label.backgroundColor = clearColor
        label.font = macLabel.font
        label.textColor = macLabel.textColor
        return label
    }()
    
    private lazy var line: UIView = {
        let view = UIView()
        view.backgroundColor = whiteColor
        return view
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        configView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func configView() {
        backgroundColor = themeColor
        selectionStyle = .none
        contentView.backgroundColor = themeColor
        contentView.addSubview(imageV)
        contentView.addSubview(nameLabel)
        contentView.addSubview(statusLabel)
        contentView.addSubview(ipLabel)
        contentView.addSubview(macLabel)
        contentView.addSubview(line)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        imageV.frame = CGRect(x: 16, y: 0, width: height / 5, height: height / 3)
        imageV.centerY = height / 2
        
        nameLabel.frame = CGRect(x: imageV.right + 16, y: 0, width: width / 2 - imageV.right - 16, height: height / 2)
        
        statusLabel.frame = nameLabel.frame
        statusLabel.top = nameLabel.bottom
        
        ipLabel.frame = CGRect(x: nameLabel.right + 2, y: 0, width: width / 2 - imageV.left - 2, height: nameLabel.height)
        
        macLabel.frame = ipLabel.frame
        macLabel.top = ipLabel.bottom
        
        line.frame = CGRect(x: 12, y: height - 1, width: width - 12 * 2, height: 1)
    }
    
    func refreshData(data:DevicesModel.Info) {
        nameLabel.text = data.hostname
        ipLabel.text = "IP Address:\n\(data.ip_addr)"
        macLabel.text = "MAC Address:\n\(data.mac_addr)"
        statusLabel.text = "Connected"
    }
}
